package com.jugheadzhou.influxdb2test.manager;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import lombok.Setter;

import java.time.Instant;
import java.util.Map;

/**
 * @author jugheadzhou
 */
public abstract class AbstractBucketApiManager {
    @Setter
    private InfluxDBClient influx2Client;
    @Setter
    private String org;
    @Setter
    private String bucket;
    @Setter
    private String measurement;

    /**
     * 写数据
     *
     * @param tagsToAdd
     * @param fieldsToAdd
     */
    public void writePoint(Map<String, String> tagsToAdd, Map<String, Object> fieldsToAdd) {
        //
        // Write data
        //
        WriteApiBlocking writeApi = influx2Client.getWriteApiBlocking();
        //
        // Write by Data Point
        //
        Point point = Point.measurement(measurement)
                .addTags(tagsToAdd)
                .addFields(fieldsToAdd)
                .time(Instant.now().toEpochMilli(), WritePrecision.MS);
        writeApi.writePoint(bucket, org, point);
    }

}
